package contstat;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        // Logger
        try {
            Logger.getLogger("").addHandler(new FileHandler("errorlog.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Look and feel
        try {
            LookAndFeelInfo[] lfs = UIManager.getInstalledLookAndFeels();
            for (LookAndFeelInfo lf : lfs) {
                System.out.println(lf.getName());
            }
            //UIManager.setLookAndFeel(new MetalLookAndFeel());
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Properties and install
        boolean PropInst = false;
        while (!PropInst) {
            boolean NeedSetup = false;
            boolean NeedTblInstall = false;
            boolean NeedTblUpdate = false;

            Vars.LoadProperties();
            if (!Vars.prop.containsKey("Username")) {
                Vars.prop.setProperty("Username", "");
            }
            if (!Vars.prop.containsKey("Userpass")) {
                Vars.prop.setProperty("Userpass", "");
            }
            if (!Vars.prop.containsKey("DB_URL")) {
                Vars.prop.setProperty("DB_URL", "");
            }
            if (!Vars.prop.containsKey("Account")) {
                Vars.prop.setProperty("Account", "");
            }
            if (Vars.prop.getProperty("Account").equals("")) {
                NeedSetup = true;
            }

            DB db = new DB();
            int TblsNumExpected = 5;
            int TblsNum = 5;
            if (db.executeNonQuery("SELECT k FROM contracts WHERE k = 0") != 0) {
                TblsNum--;
            }
            if (db.executeNonQuery("SELECT k FROM contrtypes WHERE k = 0") != 0) {
                TblsNum--;
            }
            if (db.executeNonQuery("SELECT k FROM payments WHERE k = 0") != 0) {
                TblsNum--;
            }
            if (db.executeNonQuery("SELECT k FROM users WHERE k = 0") != 0) {
                TblsNum--;
            }
            if (db.executeNonQuery("SELECT k FROM logs WHERE k = 0") != 0) {
                TblsNum--;
            }
            if (TblsNum != TblsNumExpected) {
                if (TblsNum == 0) {
                    NeedTblInstall = true;
                } else {
                    NeedTblUpdate = true;
                }
            }

            if (!NeedSetup && !NeedTblUpdate && !NeedTblInstall) {
                PropInst = true;
            } else {
                InstallDialog dlg = new InstallDialog(null, true, NeedSetup, NeedTblInstall, NeedTblUpdate);
                dlg.setLocationRelativeTo(null);
                dlg.setVisible(true);
                if (dlg.Result.equals("Exit")) {
                    return;
                }
                if (dlg.Result.equals("Skip")) {
                    PropInst = true;
                }
                if (dlg.Result.equals("Ok")) {
                    PropInst = true;
                }
            }
        }

        MainFrame frm = new MainFrame();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
